{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "933c8c94",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b9473e0",
   "metadata": {},
   "source": [
    "使用ReLU、Dropout是AlexNet（2012年）的经典方法。但是当前的这个设计仍然并非是深层的网络。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a31104ee-aed6-4b4e-994e-06b79220eb36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# convolutional neural network (2 convolutional layers)\n",
    "class ConvNet(nn.Module):\n",
    "    def __init__(self, num_classes=10):\n",
    "        super(ConvNet, self).__init__()\n",
    "        self.layer1 = nn.Sequential(\n",
    "            # 尺寸不变，[1, 16, 28, 28]，特征增强\n",
    "            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),\n",
    "            nn.BatchNorm2d(16),\n",
    "            nn.ReLU(),\n",
    "            # 尺寸减半，[1, 16, 14, 14]\n",
    "            nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "        self.layer2 = nn.Sequential(\n",
    "            # 尺寸不变，[1, 32, 14, 14]，特征增强\n",
    "            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),\n",
    "            nn.BatchNorm2d(32),\n",
    "            nn.ReLU(),\n",
    "            # 尺寸减半，[1, 32, 7, 7]\n",
    "            nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "        # [1, 1568] → [1, 10]（10 个分类）\n",
    "        self.fc = nn.Linear(7*7*32, num_classes)\n",
    "\n",
    "    def forward(self, x):\n",
    "        out = self.layer1(x)\n",
    "        out = self.layer2(out)\n",
    "        # 展平：[1, 32, 7, 7] → [1, 32×7×7] = [1, 1568]\n",
    "        out = out.reshape(out.size(0), -1)\n",
    "        out = self.fc(out)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "84d4f679-98f5-439a-93fe-a2b145a53fa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入形状: torch.Size([1, 1, 28, 28])\n",
      "输出形状: torch.Size([1, 10])\n",
      "输出结果: tensor([[-0.9645,  0.8544,  0.2105,  1.4636, -0.6517, -0.8593, -0.6315,  0.0158,\n",
      "          0.6164, -0.8622]])\n"
     ]
    }
   ],
   "source": [
    "# 创建模型实例\n",
    "model = ConvNet(num_classes=10)\n",
    "\n",
    "# 构造测试输入：批量大小为1，通道数为1，高度和宽度为28×28\n",
    "test_input = torch.randn(1, 1, 28, 28)  # 形状：[1, 1, 28, 28]\n",
    "\n",
    "# 前向传播测试\n",
    "with torch.no_grad():\n",
    "    output = model(test_input)\n",
    "    \n",
    "print(f\"输入形状: {test_input.shape}\")\n",
    "print(f\"输出形状: {output.shape}\")\n",
    "print(f\"输出结果: {output}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
